package cn.sofwin.blog.utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class HttpHelper {

    /**
     * 设置请求对象的字符集
     * @param request
     * @throws UnsupportedEncodingException
     */
    public static void  charset(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
    }

    /**
     * 设置相应对象的字符集
     * @param response
     * @throws UnsupportedEncodingException
     */
    public static void charset(HttpServletResponse response) throws UnsupportedEncodingException{
        response.setCharacterEncoding("UTF-8");
    }


    /**
     * 跳转到错误界面
     * @param msg
     * @param request
     * @param response
     */
    public static void error(String msg, HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("msg",msg);
        try {
            request.getRequestDispatcher("/error.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
